package com.example.chenzhiran.myapplication7;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.chenzhiran.myapplication7.server.clientData;
import com.example.chenzhiran.myapplication7.server.serverData;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class PurchasedActivity extends AppCompatActivity {

    //gridview
    private GridView mProductGridView;
    private List<ProductInfo> mProductInfos;

    //底部的两个按钮
    public ImageButton mSearchButton;
    private ImageButton mLikeButton;

    clientData outdata6 = new clientData();
    String username = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchased);

        //设置标题
        setTitle("我的购买");

        outdata6.pageId = 6;

        //前一页面传递信息
        Intent intent = getIntent();
        if (intent != null) {
            username = intent.getStringExtra("username");
            outdata6.username = username;
            new PurchasedActivity.connection().execute(outdata6);
        }

        //1 设置gridview

        mProductGridView = (GridView) findViewById(R.id.purchased_grid_view);
        mProductInfos = new ArrayList<>();
        final GridViewAdapter productAdapter = new GridViewAdapter(PurchasedActivity.this, mProductInfos);
        mProductGridView.setAdapter(productAdapter);

        //gridview的点击事件
        mProductGridView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(PurchasedActivity.this, ProductActivity.class);
                RelativeLayout relativeLayout = (RelativeLayout) mProductGridView.getAdapter().getView(position, view, null);
                TextView productid= (TextView) relativeLayout.getChildAt(3);
                intent.putExtra("productid",productid.getText().toString());
                startActivity(intent);
            }


        });



        //2 设置底部按钮

        //找到按钮

        mSearchButton = (ImageButton)this.findViewById(R.id.search_layout_imageButton);
        mLikeButton = (ImageButton)this.findViewById(R.id.like_layout_imageButton);

        //设置点击事件

        mSearchButton.setOnClickListener(mOnClickListener);
        mLikeButton.setOnClickListener(mOnClickListener);
    }

    //点击事件
    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch(v.getId()) {

                //底部跳转按钮
                case R.id.search_layout_imageButton:
                    Intent intent2 = new Intent(PurchasedActivity.this, MainActivity.class);

                    startActivity(intent2);
                    break;
                case R.id.like_layout_imageButton:
                    Intent intent3 = new Intent(PurchasedActivity.this, MyActivity.class);
                    startActivity(intent3);
                    break;
            }

        }
    };

    //连接
    public class connection extends AsyncTask<clientData, Void, serverData>
    {
        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
        }

        @Override
        protected serverData doInBackground(clientData... params)
        {
            clientData data = params[0];
            serverData indata = new serverData();
            try
            {
                Socket socket = new Socket("103.242.1.105", 1080);
                ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                out.writeObject(data);
                out.flush();
                ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                indata = (serverData) in.readObject();
                in.close();
                out.close();
                socket.close();
            }catch (Exception e)
            {
                e.printStackTrace();
            }
            return indata;
        }

        //返回的商品信息
        @Override
        protected void onPostExecute(serverData data)
        {
            super.onPostExecute(data);

            //System.out.println(data.imgs.size());

            for(final String key : data.imgs.keySet())
            {
                Bitmap bitmap = BitmapFactory.decodeByteArray(data.imgs.get(key), 0,
                        data.imgs.get(key).length);

                mProductInfos.add(new ProductInfo(data.productdescribes.get(key), data.productprices.get(key),bitmap,key));

            }

        }
    }
}
